package sw.EngAdroid;

import java.util.ArrayList;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.util.DisplayMetrics;
import android.view.Window;

public class SwEngAndroidActivity extends Activity {

	private Button singleplayer; //Contains button that initiates a single player game
	private Button multiplayer;
	private Button exit;
	private Button faq;
	
	public boolean small;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		DisplayMetrics dm = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics(dm);

		if(dm.widthPixels<1000)
			setContentView(R.layout.main2);
		else
			setContentView(R.layout.main);
		initControls();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void initControls() {
		
		singleplayer = (Button)findViewById(R.id.singlePlayer);
		multiplayer = (Button) findViewById(R.id.startGameButton);
		exit = (Button) findViewById(R.id.exitGameButton);
		faq = (Button) findViewById(R.id.faqButton);
		
		//Set the listener for the button
    	singleplayer.setOnClickListener(new Button.OnClickListener() { 
    		//Prompt for user name and start Game Activity
    		public void onClick (View v){ 
    			
    			//Create alert
    			AlertDialog.Builder userName = new AlertDialog.Builder(v.getContext());

    			//Set title and message
    			userName.setTitle("Player Name");
    			userName.setMessage("Enter Your name");
    			
    			//Set up text field
    			final EditText inputField = new EditText(v.getContext());
    			userName.setView(inputField);
    			
    			final View temp = v;
    			final Bundle userInfo = new Bundle();
    			
    			//Set up a confirm button in alert
    			userName.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    				//Packages the user Name into a bundle and passes it into the gameActivity
    				public void onClick(DialogInterface dialog, int whichButton) {
    					
    					String userName = inputField.getText().toString().replaceAll("\n", "");
    					userInfo.putString("userName", userName);
    					numPlayers(temp);
      				}
    				private void numPlayers(final View v){
    					AlertDialog.Builder numberOfPlayers = new AlertDialog.Builder(v
    							.getContext());
    	    			numberOfPlayers.setTitle("Number of computer players");
    	    			numberOfPlayers.setMessage("How many computer players (2-5)?");
    	    			final EditText compPlayers = new EditText(v.getContext());
    	    			numberOfPlayers.setView(compPlayers);
    	    			final View temp2 = v;
    	    			
    	    			numberOfPlayers.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    	    				//Packages the user Name into a bundle and passes it into the gameActivity
    	    				public void onClick(DialogInterface dialog, int whichButton) {
    	    					String numOfPlayers = compPlayers.getText().toString().replaceAll("\n", "");
    							if (numOfPlayers.matches("2|3|4|5")) {
    								int num = Integer.parseInt(numOfPlayers);
    								userInfo.putInt("comps", num);
    								Intent intent = new Intent(getApplication(), SinglePlayerActivity.class);
    								intent.putExtras(userInfo);
    								startActivity(intent);
    							}
    							else {
    								// Create alert
    								AlertDialog.Builder invalid = new AlertDialog.Builder(temp.getContext());
    								// Set title and message
    								invalid.setTitle("Error");
    								invalid.setMessage("Invalid number of players.");
    								invalid.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    									// Packages the user Name into a bundle and passes it into the gameActivity
    									public void onClick(DialogInterface dialog, int whichButton) {
    										numPlayers(temp2);
    									}
    								});
    								invalid.show();
    							}
    	      				}
    	    			});
    	    			numberOfPlayers.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	      			  public void onClick(DialogInterface dialog, int whichButton) {
    	      			  }
    	      			});
    	    			
    	    			numberOfPlayers.show();
					}
    			});
    			
    			//Set up cancel button that exits alert and does nothing
    			userName.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			  public void onClick(DialogInterface dialog, int whichButton) {
    			  }
    			});
    			
    			//Show alert
    			userName.show();
    			
    		}
    	});
		

		multiplayer.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				final View temp = v;
				numPlayers2(temp);
			}
				private void numPlayers2(final View v){
				// Create alert for number of players
				final AlertDialog.Builder numberOfPlayers = new AlertDialog.Builder(v
						.getContext());

				// Set title and message
				numberOfPlayers.setTitle("Number of Players");
				numberOfPlayers.setMessage("How many players (3-6)?");

				// Set up text field
				final EditText inputField = new EditText(v.getContext());
				numberOfPlayers.setView(inputField);

				final View temp = v;
				final Bundle userInfo = new Bundle();
				
				// Set up a confirm button in alert
				numberOfPlayers.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
					// Packages the user Name into a bundle and passes
					// it into the gameActivity
					public void onClick(DialogInterface dialog, int whichButton) {
						String numOfPlayers = inputField.getText().toString().replaceAll("\n", "");
						if (numOfPlayers.matches("3|4|5|6")) {
							int num = Integer.parseInt(numOfPlayers);
							promptsPlayersForNames(temp, num);
						} 
						else {
							// Create alert
							AlertDialog.Builder invalid = new AlertDialog.Builder(temp.getContext());
							// Set title and message
							invalid.setTitle("Error");
							invalid.setMessage("Invalid number of players.");
							invalid.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
								// Packages the user Name into a bundle and passes it into the gameActivity
								public void onClick(DialogInterface dialog, int whichButton) {
									numPlayers2(temp);
								}
							});
							invalid.show();
						}
					}

				private void promptsPlayersForNames(final View v, int num) {
					ArrayList<EditText> names = new ArrayList<EditText>();
					// Create alert
					AlertDialog.Builder userName = new AlertDialog.Builder(v.getContext());

					// Set title and message
					userName.setTitle("Players");
					userName.setMessage("Enter Player's Names:");

					LinearLayout linearLayout = new LinearLayout(SwEngAndroidActivity.this);
					linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
					linearLayout.setOrientation(1);

					for (int i = 0; i < num; i++) {
						names.add(new EditText(v.getContext()));
						TextView t = new TextView(v.getContext());
						t.setText("Player " + (i + 1));
						linearLayout.addView(t);
						linearLayout.addView(names.get(i));
					}
					userName.setView(linearLayout);

					final ArrayList<EditText> name = names;
					final Bundle info = new Bundle();
					// Set up a confirm button in alert
					userName.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
								// Packages the user Name into a bundle and passes it into the gameActivity
						public void onClick(DialogInterface dialog, int whichButton) {
							ArrayList<String> userNames = new ArrayList<String>();
							for (int i = 0; i < name.size(); i++) {
								userNames.add(name.get(i).getText().toString().replaceAll("\n",""));
							}
							info.putStringArrayList("playerNames", userNames);
							if(userNames.size() == 3){
								Intent intent = new Intent(getApplication(), ThreePlayerActivity.class);
								intent.putExtras(info);
								startActivity(intent);
							}
							else if(userNames.size() == 4){
								Intent intent = new Intent(getApplication(), FourPlayerActivity.class);
								intent.putExtras(info);
								startActivity(intent);
							}
							else if(userNames.size() == 5){
								Intent intent = new Intent(getApplication(), FivePlayerActivity.class);
								intent.putExtras(info);
								startActivity(intent);
							}
							else if(userNames.size() == 6){
								Intent intent = new Intent(getApplication(), SixPlayerActivity.class);
								intent.putExtras(info);
								startActivity(intent);
							}
						}
					});
					// Set up cancel button that exits alert and
					// does nothing
					userName.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {}
					});
					// Show alert
					userName.show();
				}
			});
				// Set up cancel button that exits alert and does nothing
				numberOfPlayers.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {}
				});
				
				//Create alert for local or network game
				AlertDialog.Builder localOrNetwork = new AlertDialog.Builder(v.getContext());
				localOrNetwork.setMessage("Local or Network Game?");
				localOrNetwork.setPositiveButton("Local", new DialogInterface.OnClickListener(){

					//Actions for when Local is selected
					public void onClick(DialogInterface dialog, int which) {
						// Show alert for number of players
						numberOfPlayers.show();
					}
					
				});
				localOrNetwork.setNegativeButton("Network", new DialogInterface.OnClickListener(){

					public void onClick(DialogInterface dialog, int which) {
						//Actions for when network is selected
						Intent intent = new Intent(getApplication(), LobbySelectActivity.class);
						startActivity(intent);
					}
					
				});
				
				//Show alert for local or network
				localOrNetwork.show();
			} // end of numPlayers2()
		
		});  // end of multiplayer setonclicklistener

		exit.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		faq.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				Intent intent = new Intent(getApplication(), FaqActivity.class);
				startActivity(intent);
			}
		});
	}
}